package cqrtplm.service.impl;

import com.ty.basic.entity.org.UserDO;
import com.ty.basic.service.snow.SnowflakeIdComponent;
import com.ty.basic.session.TyAccountContext;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.entity.RtCertCombCertDO;
import cqrtplm.entity.RtCertEngineFamilyDO;
import cqrtplm.mapper.RtCertEngineFamilyMapper;
import cqrtplm.service.IRTLogDifferenceService;
import cqrtplm.service.RtCertEngineFamilyService;
import cqrtplm.util.RTLogHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RtCertEngineFamilyServiceImpl implements RtCertEngineFamilyService {

    @Resource
    private RtCertEngineFamilyMapper rtCertEngineFamilyMapper;

    @Resource
    private RTLogDifferenceServiceImpl rtlogdifferenceService;

    @Resource
    private SnowflakeIdComponent snowflakeIdComponent;

    @Resource
    private RTLogHelper rtLogHelper;

    @Override
    public List<RtCertEngineFamilyDO> selectRtCertEngineFamilyList(RtCertEngineFamilyDO rtCertEngineFamily) {
        return rtCertEngineFamilyMapper.selectRtCertEngineFamilyList(rtCertEngineFamily);
    }

    @Override
    public RtCertEngineFamilyDO selectRtCertEngineFamilyById(String id) {
        return rtCertEngineFamilyMapper.selectRtCertEngineFamilyById(id);
    }

    @Override
    public int insertRtCertEngineFamily(RtCertEngineFamilyDO rtCertEngineFamily) {
        List<RtCertEngineFamilyDO> old = rtCertEngineFamilyMapper.selectRtCertEngineFamilyListByDO(rtCertEngineFamily);
        if (!old.isEmpty()) {
            throw RTErrorCodeEnum.CERT_NAME_EXISTS.getException("该组合认证已存在,请勿重复添加");
        } else {

            UserDO user = TyAccountContext.getUser();
            if (user == null) {
                throw new RuntimeException("用户未登录");
            }

            String snowId = String.valueOf(snowflakeIdComponent.getInstance().nextId());
            rtCertEngineFamily.setId(snowId);

            log.info("发动机系族-新增{},{},{}",
                     rtCertEngineFamily.getFamilyName(),
                     snowId,
                     user.getName());

            return rtCertEngineFamilyMapper.insertRtCertEngineFamily(rtCertEngineFamily);
        }
    }

    @Override
    public int updateRtCertEngineFamily(RtCertEngineFamilyDO rtCertEngineFamily) {
        RtCertEngineFamilyDO old = rtCertEngineFamilyMapper.selectRtCertEngineFamilyById(rtCertEngineFamily.getId());
        if(old == null) {
            throw RTErrorCodeEnum.DATA_NOT_EXISTS.getException("该发动机系族不存在");
        }
        else{
            UserDO user = TyAccountContext.getUser();
            if (user == null) {
                //return ResponseResult.fail("401", "用户未登录");
                throw new RuntimeException("用户未登录");
            }

            Timestamp timestamp = new Timestamp(new Date().getTime());

            int r=rtCertEngineFamilyMapper.updateRtCertEngineFamily(rtCertEngineFamily);
            if(r>0){
                rtlogdifferenceService.newLog(
                        rtCertEngineFamily,old,
                        rtCertEngineFamily.getId(),
                        user.getName(),
                        "表单",
                        "修改",
                        ""
                );
            }
            return r;
        }
    }

    @Override
    public int deleteRtCertEngineFamilyById(String id) {
        return rtCertEngineFamilyMapper.deleteRtCertEngineFamilyById(id);
    }

    @Override
    public int deleteRtCertEngineFamilyByIds(List<String> ids) {
        return rtCertEngineFamilyMapper.deleteRtCertEngineFamilyByIds(ids);
    }
}